package saint.UI;

import saint.manage.Convert;
import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;



public abstract class DetailView extends View {
	// Private field
	private RectF mBounds;
	private Paint bgPaint;
	private Paint lbPaint;
	private Paint valuePaint;
	private Paint unitPaint;
	private PointerLayout labelStart;
	private PointerLayout valueStart;
	private PointerLayout unitStart;
	private float mRoundConner;	
	private int mHeight;
	private int mWidth;
	
	// Protected field
	protected String mLabelText = "Label";
	protected String mValueText = "00.0";
	protected String mUnitText = "Unit";
	

	
	//============ Constructor ===============================================//
	public DetailView(Context context) {
 		super(context);
 		init();
 	}
 	
	public DetailView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	//============ Public Method ============================================//
	public void setValue(double value) {
		mValueText = String.format("%.1f", value);
		invalidate();
	}

	public void setUnit(String text) {
		mUnitText = text;
		invalidate();
	}
	
	public void setHeigth(int value) {
		mHeight = Convert.dpToPx(value, getContext());;
		invalidate();
	}
	
	public void setWidth(int value) {
		mWidth = Convert.dpToPx(value, getContext());;
		invalidate();
	}
	
	//============ Protected Method =========================================//
	protected void init() {
		
		//Set dimension
		mHeight = Convert.dpToPx(80, getContext());
		mWidth = Convert.dpToPx(100, getContext());
		mRoundConner = (float)Convert.dpToPx(10, getContext()); 		
		
		//Set background
		mBounds = new RectF(0, 0, mWidth, mHeight);
		bgPaint = new Paint();
		bgPaint.setColor(Color.BLACK);
		bgPaint.setAlpha(125);	// 50 Opacity
		
		//Label init
		lbPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		labelStart = new PointerLayout();	
		lbPaint.setColor(Color.WHITE);

		valuePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		valueStart = new PointerLayout();
		
		valuePaint.setColor(Color.WHITE);
		valuePaint.setTextAlign(Paint.Align.CENTER);
		
		unitPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		unitStart = new PointerLayout();
		
		unitPaint.setColor(Color.WHITE);
		unitPaint.setTextAlign(Paint.Align.RIGHT);
		
		setSize();
		
	}
	
	protected void setLabel(String text) {
		mLabelText = text;
		invalidate();
	}
	
	protected void setRoundConner(float value) {
		mRoundConner = value;
		invalidate();
	}
	
	protected abstract void initView();
	
	protected void setSize() {
		lbPaint.setTextSize((float) (mHeight * 0.25)); 
		labelStart.startX = (int) (mWidth * 0.1);
		labelStart.startY = (int) ((mHeight * 0.05) + lbPaint.getTextSize());
		
		valuePaint.setTextSize((float) (mHeight * 0.45));
		valueStart.startX = (int) (mWidth / 2);
		valueStart.startY = (int) (labelStart.startY + valuePaint.getTextSize());
		
		unitPaint.setTextSize((float) (mHeight * 0.15));
		unitStart.startX = (int) (mWidth * 0.9);
		unitStart.startY = (int) (valueStart.startY + unitPaint.getTextSize());
		/*
		float lbWidth = lbPaint.measureText(mLabelText);
		float vW = valuePaint.measureText(mValueText);
		float uw = unitPaint.measureText(mLabelText);
		
		if(vW > (int)(mWidth * 0.8)) {
			lbPaint.setTextSize((float))
		}*/
	}
	//============ Private Method ===========================================//
	
	
	//============ Override Method ==========================================//
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh ) {
		super.onSizeChanged (w, h, oldw, oldh);
		mBounds.set(0, 0, w, h);
		setSize();
		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawRoundRect(mBounds, mRoundConner, mRoundConner, bgPaint);
		canvas.drawText(mLabelText, labelStart.startX, labelStart.startY, lbPaint);
		canvas.drawText(mValueText, valueStart.startX, valueStart.startY, valuePaint);
		canvas.drawText(mUnitText, unitStart.startX, unitStart.startY, unitPaint);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(mWidth, mHeight);
	}


}
